/*
 * This example will be a bit confusing
 */

package collections;
import java.util.*;
/**
 *
 * @author Iskandar Sarkawi
 */
public class LinkedListExample1 {
    
    public static void main(String[] args) {

        String colors1[] = {"black", "yellow", "green","blue"};
        String colors2[] = {"white", "gold", "brown"};

        LinkedList<String> list1 = new LinkedList<String>();
        LinkedList<String> list2 = new LinkedList<String>();

        //add elemements to list
        for(String x: colors1)
            list1.add(x);

        //add elemements to list
        for(String x: colors2)
            list2.add(x);

        list1.addAll(list2); //concatenate list

        printMe(list1);
        removeStuff(list1, 2, 5);
        printMe(list1);
        reverseMe(list1);

    }

    public static void printMe(LinkedList<String> list){
        for(String x: list)
            System.out.printf("%s ", x);
        System.out.println();
    }

    public static void removeStuff(LinkedList<String> list, int from, int to){
        list.subList(from, to).clear();
    }

    public static void reverseMe(LinkedList<String> list){
        ListIterator<String> iterator = list.listIterator(list.size());
        /*
         Returns a list-iterator of the elements in this list (in proper sequence), 
         starting at the specified position in the list. 
         Obeys the general contract of List.listIterator(int).
         */

        while(iterator.hasPrevious())
            System.out.printf("%s ", iterator.previous());
        /*
         An iterator for lists that allows the programmer to traverse the list in either direction,
         modify the list during iteration, and obtain the iterator's current position in the list.
         */
    }
}
